
# XXX test
GENODE_DIR := ~/src/genode

VERBOSE ?= @

ifeq ($(GENODE_DIR),)
$(error GENODE_DIR not specified)
endif

#define BASE_INCLUDE_DIRS
#  ram_session
#  base
#  util
#  32bit
#  64bit
#endef

define BASE_INCLUDE_DIRS
  base
  cap_session
  cpu
  cpu_session
  dataspace
  io_mem_session
  io_port_session
  irq_session
  log_session
  parent
  pd_session
  ram_session
  rm_session
  rom_session
  root
  session
  signal_session
  thread
  trace_session
  util
endef

#define OS_INCLUDE_DIRS
#  audio_out_session
#  block_session
#  file_system_session
#  framebuffer_session
#  gpio_session
#  init
#  input_session
#  loader_session
#  nic_session
#  nitpicker_session
#  os
#  packet_stream_rx
#  packet_stream_tx
#  pci_device
#  pci_session
#  regulator_session
#  report_session
#  rtc_session
#  terminal_session
#  timer_session
#  uart_session
#  usb_session
#  util
#endef

GENODE_ABS_DIR := $(realpath $(shell echo $(GENODE_DIR)))

API_DIRS := $(addprefix repos/base/include/,$(BASE_INCLUDE_DIRS)) \
            $(addprefix repos/os/include/,$(OS_INCLUDE_DIRS))

SRC_H := $(shell cd $(GENODE_DIR); \
             for i in $(API_DIRS); do \
                 find $$i -name "*.h"; done)

# list of token files to generate
TOKEN_FILES := $(SRC_H:.h=.h.tokens)

# include class index prior generated via the rule below
-include class_index.mk

usage:
	@echo --- available targets ---
	@echo class_index.mk     - generate class index
	@echo class_descriptions - generate class descriptions
	@echo file_overviews     - generate file overviews
	@echo
	@echo When invoking this Makefile, the GENODE_DIR variable must be
	@echo set to the base directory of the Genode source tree.

#
# Generate class index
#
class_index.mk: $(TOKEN_FILES) Makefile gen_class_index util.tcl
	$(VERBOSE)echo "# generated by ./gen_class_index" > $@
	@for i in $(TOKEN_FILES); do \
		echo "generate class index of $$i"; \
		./gen_class_index $$i >> $@ ;done

CLASS_DESCRIPTIONS := $(addprefix classes/,$(addsuffix /description.tex,$(CLASSES)))

$(CLASS_DESCRIPTIONS) : Makefile gen_class_description util.tcl latex.tcl

class_descriptions: $(CLASS_DESCRIPTIONS)

classes/%/diagram.tikz:
	$(VERBOSE)mkdir -p $(dir $@)
	@echo "generate class diagram for ${CLASS_NAME($*)}"
	$(VERBOSE)./gen_class_diagram --class "${CLASS_NAME($*)}" ${CLASS_HEADER($*)}.tokens > $@

classes/%/description.tex:
	$(VERBOSE)mkdir -p $(dir $@)
	@echo "generate class description for ${CLASS_NAME($*)}"
	$(VERBOSE)./gen_class_description --class "${CLASS_NAME($*)}" ${CLASS_HEADER($*)}.tokens > $@

$(SRC_H):
	$(VERBOSE)mkdir -p $(dir $@)
	$(VERBOSE)ln -sf $(GENODE_ABS_DIR)/$@ $@

%.h.tokens: %.h
	@echo "extract tokens from $^"
	$(VERBOSE)$(GENODE_ABS_DIR)/tool/parse_cxx -format tokens $^ > $@

FILE_OVERVIEW_HEADERS := repos/base/include/util/string.h \
                         repos/base/include/util/construct_at.h \
                         repos/base/include/base/blocking.h \
                         repos/base/include/base/cache.h \
                         repos/base/include/base/printf.h \
                         repos/base/include/base/env.h \
                         repos/base/include/base/snprintf.h \
                         repos/base/include/base/stdint.h \
                         repos/base/include/64bit/base/fixed_stdint.h \
                         repos/base/include/32bit/base/fixed_stdint.h \
                         repos/base/include/ram_session/ram_session.h \
                         repos/base/include/rom_session/rom_session.h \
                         repos/base/include/io_mem_session/io_mem_session.h

FILE_OVERVIEWS := $(addsuffix .overview.tex,$(FILE_OVERVIEW_HEADERS))

file_overviews: $(FILE_OVERVIEWS)

%.h.overview.tex: %.h.tokens
	@echo "generate file overview for $*"
	$(VERBOSE)./gen_file_overview $< > $@

